package cn.wolfcode.ssm.web.controller;


import cn.wolfcode.ssm.domain.Business;
import cn.wolfcode.ssm.domain.Department;
import cn.wolfcode.ssm.qo.BusinessQueryObject;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.service.IBusinessService;
import cn.wolfcode.ssm.util.DateUtil;
import cn.wolfcode.ssm.util.FileUploadUtil;
import cn.wolfcode.ssm.util.RequireAnnotation;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;

@Controller
@RequestMapping("/business")
public class BusinessController {

    @Autowired
    private IBusinessService businessService;
    @Autowired
    private ServletContext servletContext;


    @RequireAnnotation(name = "门店页面",expression = "business:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")BusinessQueryObject qo){
        PageInfo<Business> pageInfo = businessService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "business/list";
    }

    @RequestMapping("/delete")
    @RequireAnnotation(name = "门店删除",expression = "business:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            businessService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequireAnnotation(name = "门店新增/编辑",expression = "business:saveOrUpdate")
    public String saveOrUpdate(MultipartFile file,Business business) throws Exception {
        //当有id，即编辑的时候，没有上传文件，保证update语句的执行
        //需要在sql中对插入图片列进行if判断
        //判断是否有传入文件
        if(file != null && file.getSize()>0){
            //获取文件绝对路径
            String realPath = servletContext.getRealPath("/");
            if( business.getLicenseImg() != null){
                //利用工具类先将原先的文件删除
                FileUploadUtil.deleteFile(realPath + business.getLicenseImg());
            }
            //利用工具类将文件保存在服务器中
            String url = FileUploadUtil.uploadFile(file, realPath);
            business.setLicenseImg(url);
        }
        if (business.getId() != null) {
            businessService.update(business);
        }else {
            businessService.save(business);
        }
        return "redirect:/business/list";
    }


    @RequireAnnotation(name = "门店编辑/保存",expression = "business:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Model model,Long id){

        if(id != null){
            Business business = businessService.get(id);
            model.addAttribute("business",business);
        }
        return "business/input";
    }
}
